package com.example.springboot159.controller.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springboot159.annotation.XssRequired;
import com.example.springboot159.bean.BaseResult;
import com.example.springboot159.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RestController
@RequestMapping(value = "/login")
public class LoginController {
    private static  final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @GetMapping(value = "/testUser")
    @XssRequired
    public BaseResult testUser(Map<String,Object> maps){
        logger.info("maps:"+maps);
        return BaseResult.buildResult("0","success",null);
    }

    @GetMapping(value = "/testLogin")
    public BaseResult testLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BaseResult result =  BaseResult.buildResult("A00001","success",null);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(result));
        response.flushBuffer();
        return BaseResult.buildResult("A00004","未登录","null");
    }

    @GetMapping(value = "/testLogin2")
    public BaseResult testLogin2(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BaseResult result =  BaseResult.buildResult("A00001","success",null);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(result));
        out.flush();
        out.close();
        return BaseResult.buildResult("A00004","未登录","null");
    }


    @GetMapping(value = "/testException")
    public BaseResult testException(Integer type)  {
        BaseResult result = new BaseResult();
        result.setErrMsg("msg");
        result.setCode("code");
        result.setData("data");
        try{
            switch (type){
                case  1:
                    result.setErrMsg("msg");
                    result.setCode("code");
                    result.setData("data");
                    throw  new BusinessException(result);
                case  2:
                    result.setErrMsg("runTime msg");
                    result.setCode("code");
                    result.setData("data");
                    throw  new RuntimeException(JSON.toJSONString(result));
                default:break;
            }
        }catch (Exception e){
            if(e instanceof  BusinessException){
                return  ((BusinessException) e).getResult();
            }
            throw  new RuntimeException(e.getMessage());
        }
        return BaseResult.buildResult("A00004","未登录","null");
    }

}
